package com.digitalchina.lxnsy.activity.mainConsult;

import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.activity.old.WebViewActivity;
import com.digitalchina.lxnsy.base.BaseListActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.LiteratureVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/23 16:24
 * 修改备注：
 * ================================================
 */
public class LiteratureListActivity extends BaseListActivity {

    private List<LiteratureVO> voList = new ArrayList<>();
    String expertId = "";

    @Override
    protected void setAdapter() {

        expertId = (String) getIntent().getSerializableExtra("expertId");

        setTitle("专家文献列表");
        setDecoration(1);

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new LiteratureHolder(parent);
            }
        });


        onRefresh();
    }

    @Override
    protected void onClick(int position) {
        super.onClick(position);
        LiteratureVO vo = (LiteratureVO) adapter.getAllData().get(position);
        Map<String, String> map = new HashMap<>();
        map.put("url", URL.URL_GET_EXPERT_LITERATURE_INFO_DETAIL_H5 + "?literatureInfoId=" + vo.getId() + "&userAccId=" + UserXML.getUserId(activity));
        map.put("title", "专家文献");
        GotoUtil.gotoActivity(activity, WebViewActivity.class, "map", map);
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0:
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "literatureInfoList", LiteratureVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        getList();
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            getList();
        }
    }


    //获取专家文献
    private void getList() {

        HttpParams params = new HttpParams();
        params.put("expertId", expertId);
        params.put("page", String.valueOf(page));
        params.put("limit", String.valueOf(limit));

        OkHttpUtil.post(activity, URL.URL_GET_EXPERT_LITERATURE_INFO_LIST, "", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

}
